//
//  SCView.m
//  CWeekly
//
//  Created by Tiger on 12-4-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SCView.h"
#import "AppDelegate.h"
#define HEIGHT_S 1024-75-58
#define HEIGHT_H 768-75-58

@implementation SCView


- (void)dealloc
{
    [table release];
    [collectionList release];
    [super dealloc];
}

- (id)initWithH:(BOOL)isH
{
    self = [super init];
    if (self) {
        // Initialization code
        
        _isH = isH;
        
        AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
        collectionList = [[NSMutableArray alloc]initWithArray:[delegate getCollection]];
        
        [[NSNotificationCenter defaultCenter] addObserver: self 
												 selector: @selector(reflashList) 
													 name: @"ReflashShoucangList" 
												   object: nil ];
        
        table = [[UITableView alloc]init];
        table.allowsSelection = NO;
        table.delegate = self;
        table.dataSource = self;
        table.backgroundColor = [UIColor clearColor];
        [self addSubview:table];
        
        UIView *v = [[UIView alloc]initWithFrame:CGRectZero];
        table.tableFooterView = v;
        [v release];
        
        if (_isH) {
            self.frame = CGRectMake(0, 55, 1024, HEIGHT_H);
            table.frame = CGRectMake(0, 0, 1024, HEIGHT_H);
            columnCount = 4;
        }
        else {
            self.frame = CGRectMake(0, 55, 768, HEIGHT_S);
            table.frame = CGRectMake(0, 0, 768, HEIGHT_S);
            columnCount = 3;
        }
    }
    return self;
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        self.frame = CGRectMake(0, 55, 1024, HEIGHT_H);
        table.frame = CGRectMake(0, 0, 1024, HEIGHT_H);
        _isH = YES;
        columnCount = 4;
    }
    else {  
        self.frame = CGRectMake(0, 55, 768, HEIGHT_S);
        table.frame = CGRectMake(0, 0, 768, HEIGHT_S);
        _isH = NO;
        columnCount = 3;
    }
    
    [table reloadData];
}

- (void)reflashList
{
    [collectionList removeAllObjects];
    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    [collectionList addObjectsFromArray:[delegate getCollection]];
    [table reloadData];
}

#pragma mark - 
#pragma mark UITableViewDatasource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 218;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
	if (_isH) {
        columnCount = 4;
    }
    else {
        columnCount = 3;
    }
    
    rowCount = ceil((CGFloat)[collectionList count]/(CGFloat)columnCount);
    
    return rowCount;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	
	static NSString* ListIdentifier = @"ListIdentifier";
	UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ListIdentifier];
	
	if (cell == nil)
	{
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ListIdentifier];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;  //去掉选中时的颜色
	}
	
	for (UIView* subView in [cell.contentView subviews])
	{
		[subView removeFromSuperview];
	}
	
	for (int i=0; i<columnCount; i++) {
        NSInteger index = indexPath.row*columnCount+i;
        
        if (index < [collectionList count]) {
            
            NSDictionary *dic = [collectionList objectAtIndex:index];
            
            CGFloat width = table.frame.size.width / columnCount;
            CGFloat x = (width - 160)/2;
            
            NSString*imgPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/BookData/%@/%@",[dic objectForKey:@"mgId"],[dic objectForKey:@"t_thumbnails"]]];
            imgPath = [imgPath stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
            NSData *data = [NSData dataWithContentsOfFile:imgPath];
            UIImage *image = [UIImage imageWithData:data];
            
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(width*i+x, 20, 160, 128)];
            btn.tag = 1000+index;
            [btn addTarget:self action:@selector(toDetailViewController:) forControlEvents:UIControlEventTouchUpInside];
            [btn setImage:image forState:UIControlStateNormal];
            [cell.contentView addSubview:btn];
            [btn release];
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(width*i, 158, width, 20)];
            label.text = [NSString stringWithFormat:@"-%d-",index+1];
            label.textAlignment = UITextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:16.0];
            label.backgroundColor = [UIColor clearColor];
            label.textColor = [UIColor whiteColor];
            [cell.contentView addSubview:label];
            [label release];
            
            label = [[UILabel alloc] initWithFrame:CGRectMake(width*i, 183, width, 20)];
            label.text = [dic objectForKey:@"time"];
            label.textAlignment = UITextAlignmentCenter;
            label.font = [UIFont systemFontOfSize:14.0];
            label.backgroundColor = [UIColor clearColor];
            label.textColor = [UIColor blackColor];
            [cell.contentView addSubview:label];
            [label release];
        }
    }
	
	cell.backgroundColor = [UIColor clearColor];
	
	return cell;
}

- (void)toDetailViewController:(UIButton *)sender
{
    NSInteger index = sender.tag - 1000;
    NSDictionary *dic = [collectionList objectAtIndex:index];
    NSMutableDictionary *object = [NSMutableDictionary dictionary];
    [object setValue:[dic valueForKey:@"mgId"] forKey:@"mgId"];
    [object setValue:[dic valueForKey:@"num"] forKey:@"num"];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"ToDetaiViewController" object:object];
}

@end
